package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.nationbasedata.NationBasedataBeanBO;
import com.handydata.antifraud.bo.nationbasedata.NationBasedataReqBO;
import com.handydata.antifraud.bo.nationbasedata.NationBasedataResBO;
import com.handydata.antifraud.common.exception.NationBasedataExCode;
import com.handydata.antifraud.dao.NationBasedataDao;
import com.handydata.antifraud.entity.NationBasedataDO;
import com.handydata.antifraud.service.NationBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("nationBasedataService")
public class NationBasedataServiceImpl extends AbsService implements NationBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(NationBasedataServiceImpl.class);
	
	@Autowired
	private NationBasedataDao nationBasedataDao;

	@Transactional
	@Override
	public NationBasedataResBO createNationBasedata(NationBasedataReqBO req) throws SysException{
	    NationBasedataResBO res = new NationBasedataResBO();
		try {
			NationBasedataDO entity = CommonBeanConvertor.convert(req, NationBasedataDO.class);
			long status = nationBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(NationBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(NationBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(NationBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public NationBasedataResBO modifyNationBasedata(NationBasedataReqBO req) throws SysException{
	    NationBasedataResBO res = new NationBasedataResBO();
		try {
			NationBasedataDO condition = CommonBeanConvertor.convert(req, NationBasedataDO.class);
			long status = nationBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(NationBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(NationBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(NationBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public NationBasedataResBO deleteNationBasedata(NationBasedataReqBO req) throws SysException{
	    NationBasedataResBO res = new NationBasedataResBO();
		
		try {
			NationBasedataDO condition = CommonBeanConvertor.convert(req, NationBasedataDO.class);
			long status = nationBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(NationBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(NationBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(NationBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public NationBasedataResBO queryNationBasedata(NationBasedataReqBO req) throws SysException{
		NationBasedataDO condition = CommonBeanConvertor.convert(req, NationBasedataDO.class);
		NationBasedataResBO res;
		
		try {
			List<NationBasedataDO> entities = nationBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, NationBasedataResBO.class, NationBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(NationBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
